
package jason.asSyntax;

import java.util.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Represents the export directive. <br />
 * <code>{export functor/arity[, functor/arity[, ...]]}</code>
 */
public class Exportive extends Atom {
	
	private final LinkedList<PredicateIndicator> preds;
	
	public Exportive(LinkedList<PredicateIndicator> preds) {
		super("export");
		this.preds = preds;
	}
	
	public Exportive(Exportive exportive) {
		super(exportive);
		this.preds = (LinkedList<PredicateIndicator>)exportive.preds.clone();
	}
	
	public LinkedList<PredicateIndicator> getPreds() {
		return preds;
	}
	
	@Override
	protected int calcHashCode() {
		int result = super.calcHashCode();
		for(PredicateIndicator e : preds) {
			result = 17 * result + e.hashCode();
		}
		return result;
	}
	
	@Override
	public Exportive clone() {
		return new Exportive(this);
	}
	
	@Override
	public Element getAsDOM(Document document) {
		document.createElement("structure");
		Element u = document.createElement("structure");
		u.setAttribute("functor", getFunctor());
		
		for(PredicateIndicator e : preds) {
			u.appendChild(e.getAsDOM(document));
		}
		
		return u;
	}
	
	@Override
	public String toString() {
		StringBuilder str = new StringBuilder("{export ");
		
		boolean first = true;
		for(PredicateIndicator e : preds) {
			if (first) first = false;
			else str.append(", ");
			str.append(e);
		}
		str.append('}');
		return str.toString();
	}
}
